#include <linux/sched.h>
#include <linux/sched/task.h>
#include <linux/string.h>
#include <linux/init.h>

static int task_kernel_clone(struct task_struct *task_new, struct kernel_clone_args *args)
{
    copy_thread(task_new, args);

    task_new->filedesc = current->filedesc;

    sched_fork(0, task_new);

    raw_spin_lock_init(&task_new->pi_lock);

    if (args->name)
        __strncpy(task_new->name, args->name, sizeof(task_new->name));

    wake_up_new_task(task_new);

    return 0;
}

int kernel_thread_init(struct task_struct *task, void *stack,
                       int (*fn)(void *), void *arg, const char *name,
                       unsigned long flags)
{
    struct kernel_clone_args cargs = {
        .fn = fn,
        .fn_arg = arg,
        .name = name,
    };

    task->stack = stack;

    return task_kernel_clone(task, &cargs);
}
